﻿
package com.ostrich.project.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.base.util.DateUtil;
import com.base.util.HtmlUtil;
import com.base.util.SessionUtils;
import com.base.web.BaseAction;
import com.ostrich.common.OstrichConstants;
import com.ostrich.manpower.bean.TostHrBaseinfo;
import com.ostrich.manpower.bean.TostHrDepartinfo;
import com.ostrich.manpower.service.impl.TostHrBaseinfoService;
import com.ostrich.manpower.service.impl.TostHrDepartinfoService;
import com.ostrich.project.bean.TostPjTeammemberinfo;
import com.ostrich.project.service.impl.TostPjTeammemberinfoService;
import com.ostrich.system.analysis.ParaminfoUtil;

/**
 * ClassName: Controller <br/>
 * date: 2013-5-9 下午03:07:38 <br/>
 * 
 * @author wangyinan
 * @version
 */
@Controller
@RequestMapping("/tostPjTeammemberinfo")
public class TostPjTeammemberinfoController extends BaseAction
{
	private final static Logger log= Logger.getLogger(TostPjTeammemberinfoController.class);

	// Servrice start
	@Autowired(required=false) //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private TostPjTeammemberinfoService<TostPjTeammemberinfo> tostPjTeammemberinfoService; 
	
	@Autowired(required=false) //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private TostHrDepartinfoService<TostHrDepartinfo> tosthrdepartinfoservice; 
	
	@Autowired(required=false) //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private TostHrBaseinfoService<TostHrBaseinfo> tosthrbaseinfoservice; 
	/**
	 * ilook 首页
	 * @param url
	 * @param classifyId
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView  list(TostPjTeammemberinfo tostPjTeammemberinfo,HttpServletRequest request) throws Exception{
		
		request.getSession().setAttribute("pjId", tostPjTeammemberinfo.getPjId());
		request.getSession().setAttribute("hdpId", tostPjTeammemberinfo.getHdpId());
		Map<String,Object>  context = getRootMap();
		List<TostPjTeammemberinfo> dataList = tostPjTeammemberinfoService.queryByList(tostPjTeammemberinfo);
		//设置页面数据
		context.put("dataList", dataList);
		context.put("pjId", tostPjTeammemberinfo.getPjId());
		context.put("hdpId", tostPjTeammemberinfo.getHdpId());
		return forword("project/tabTostPjTeammemberinfo",context); 
	}
	
	/**
	 * json 列表页面
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/dataList")
	public void  dataList(TostPjTeammemberinfo tostPjTeammemberinfo,HttpServletRequest request,HttpServletResponse response) throws Exception{
		Integer pjId=(Integer) request.getSession().getAttribute("pjId");
		Integer hdpId=(Integer) request.getSession().getAttribute("hdpId");
		List<TostPjTeammemberinfo> dataList = tostPjTeammemberinfoService.queryByList(tostPjTeammemberinfo);
		
		//设置页面数据
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		jsonMap.put("total",(dataList!=null)?dataList.size():0);
		jsonMap.put("data", dataList);
		HtmlUtil.writerJson(response, jsonMap);
		
	}


	/**
	 * 添加或修改数据
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/save")
	public ModelAndView save(TostPjTeammemberinfo bean,
			HttpServletResponse response, HttpServletRequest request)
			throws Exception {
		bean.setCreatetime(DateUtil.getNowPlusTime());
		bean.setPerson(SessionUtils.getUser(request).getEmail());
		String hdpName="";
		
		TostHrBaseinfo bs = tosthrbaseinfoservice.queryById(bean.getHrId());
		bean.setHdpId(bs.getHrDepart());
		if(bean.getHdpId()!=null){
			TostHrDepartinfo tdp= tosthrdepartinfoservice.queryById(bean.getHdpId());
			hdpName=(tdp!=null)?tdp.getHdpName():"";
		}
		
		bean.setHdpName(hdpName);
		Map<String, Object> context = getRootMap();
		try {
			if (bean.getId() == null) {
				tostPjTeammemberinfoService.add(bean);
			} else {
				tostPjTeammemberinfoService.update(bean);
			}
			context.put("success", true);
			context.put("msg", "您已经添加成功。");
			//String parm = (bean!=null&&bean.getHdpId()!=null)?"?hdpId="+bean.getHdpId():"";
			context.put("url", request.getContextPath()
					+ "/tostPjTeaminfo/list.do");
		} catch (Exception e) {
			e.printStackTrace();
			context.put("msg", "添加失败");
		}

		return forword("/penmsg", context);
	}

	
	
	
	@RequestMapping("/getId")
	public void getId(Integer id,HttpServletResponse response) throws Exception{
		Map<String,Object>  context = getRootMap();
		TostPjTeammemberinfo bean  = tostPjTeammemberinfoService.queryById(id);
		if(bean  == null){
			sendFailureMessage(response, "没有找到对应的记录!");
			return;
		}
		context.put(SUCCESS, true);
		context.put("data", bean);
		HtmlUtil.writerJson(response, context);
	}
	
	@RequestMapping("/delete")
			public void delete(Integer[] id,HttpServletResponse response) throws Exception{
				tostPjTeammemberinfoService.delete(id);
				sendSuccessMessage(response, "删除成功");
	}
	
	//查询参加项目的部门；
	@RequestMapping("/querydepartbypjid") 
		public void  querydepartbypjid(TostPjTeammemberinfo tostPjTeammemberinfo,HttpServletResponse response) throws Exception{
			List<Map<String,Object>> dataList=tostPjTeammemberinfoService.querydepartbypjid(tostPjTeammemberinfo.getPjId());
			//设置页面数据
			Map<String,Object> jsonMap = new HashMap<String,Object>();
			jsonMap.put("total",(dataList!=null)?dataList.size():0);
		    jsonMap.put("rows", dataList);
		    HtmlUtil.writerJson(response, jsonMap);
		}
	
	//查询参加项目的部门；
	@RequestMapping("/querypersonbypjid") 
		public void  querypersonbypjid(TostPjTeammemberinfo tostPjTeammemberinfo,HttpServletResponse response) throws Exception{
			List<Map<String,Object>> dataList=tostPjTeammemberinfoService.querypersonbypjid(tostPjTeammemberinfo.getPjId());
			//设置页面数据
			Map<String,Object> jsonMap = new HashMap<String,Object>();
			jsonMap.put("total",(dataList!=null)?dataList.size():0);
		    jsonMap.put("rows", dataList);
		    HtmlUtil.writerJson(response, jsonMap);
		}
}
